package com.lcm.openapi.core;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;

import java.util.Objects;
import java.util.Set;

public class GenerateUtil {

    public static String JSON_CIRCLE_KEY = "$ref";
    public static String JAVA_TYPE_LIST = "List";

    /**
     * 转换成java类型
     * @param type openapi字段类型
     * @return java类型
     */
    public static String toJavaType(String type){
        if(StrUtil.isEmpty(type)){
            return null;
        }
        String javaType = null;
        switch (type) {
            case "string" :
                javaType = "String";
                break;
            case "integer" :
                javaType = "Integer";
                break;
            case "object" :
                javaType = "Object";
                break;
            case "number" :
                javaType = "BigDecimal";
                break;
            case "array" :
                javaType = "List";
                break;
            case "boolean" :
                javaType = "boolean";
                break;
        }
        return javaType;
    }

    /**
     * 寻找java类型所依赖的包名
     * @param type openapi字段类型
     * @return java依赖包
     */
    public static String findDependency(String type){
        if(StrUtil.isEmpty(type)){
            return null;
        }
        String packageName = null;
        switch (type) {
            case "number":
            case "BigDecimal" :
                packageName = "import java.math.BigDecimal;";
                break;

            case "array":
            case "List" :
                packageName = "import java.util.List;";
                break;
        }
        return packageName;
    }

    /**
     * 从json中解析具体ref(一般指具体的模型)
     * @param json json数据
     * @return java依赖包
     */
    public static String analyseRef(JSONObject json) {
        String ref = null;
        if (Objects.nonNull(json)) {
            ref = json.getString(JSON_CIRCLE_KEY);
            if (StrUtil.isNotEmpty(ref)) {
                ref = ref.substring(ref.lastIndexOf("/") + 1);
            }
        }
        return ref;
    }

    /**
     * 解析java list泛型
     * @param javaType java类型
     * @param json json对象
     * @return 泛型对象
     */
    public static String transJavaListGeneric(String javaType , JSONObject json , Set<String> refModels){
        if(GenerateUtil.JAVA_TYPE_LIST.equals(javaType)){
            JSONObject items = json.getJSONObject("items");
            if(CollectionUtil.isNotEmpty(items)){
                //依赖模块
                String ref = analyseRef(items);
                if(StrUtil.isNotEmpty(ref)){
                    //若依赖其他模块,则泛型类型为指向模型
                    javaType = "List<" + ref + ">";
                    refModels.add(ref);
                }else{
                    //泛型类型为java基本类型
                    String itemType = toJavaType(items.getString("type"));
                    if(StrUtil.isNotEmpty(itemType)){
                        javaType = "List<" + itemType + ">";
                    }
                }
            }
        }
        return javaType;
    }
}
